package com.twu.biblioteca;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class LibraryView {
    private Scanner reader;
    private PrintStream printer;
    private Library library;
    private SessionManager sessionManager;

    public LibraryView(Scanner reader, PrintStream printer, Library library, SessionManager sessionManager) {
        this.reader = reader;
        this.printer = printer;
        this.library = library;
        this.sessionManager = sessionManager;
    }

    public void displayAvailableBookList() {
        printer.println(library.displayAvailableBookList());
    }

    public void checkoutBook() {
        reader.nextLine();
        printer.println("Please enter the name of the book you wish to checkout");
        if(library.checkoutBookByTitle(reader.nextLine())) {
            printer.println("Thank you! Enjoy the book");
        } else {
            printer.println("That book is not available");
        }
    }

    public void returnBook() {
        reader.nextLine();
        printer.println("Please enter the name of the book you wish to return");
        if (library.returnBookByTitle(reader.nextLine())) {
            printer.println("Thank you for returning the book");
        } else {
            printer.println("That is not a valid book to return");
        }
    }

    public void displayAvailableMovieList() {
        printer.println(library.displayAvailableMovieList());
    }

    public void checkoutMovie() {
        reader.nextLine();
        printer.println("Please enter the name of the movie you wish to checkout");
        if(library.checkoutMovieByTitle(reader.nextLine())) {
            printer.println("Thank you! Enjoy the movie");
        } else {
            printer.println("That movie is not available");
        }
    }

    public void login() {
        reader.nextLine();
        printer.println("Please enter your username");
        String username = reader.nextLine();
        printer.println("Please enter your password");
        sessionManager.loginUser(username, reader.nextLine());
        if(sessionManager.isAuthenticated()) {
            printer.println("You have been successfully logged in");
        } else {
            printer.println("Invalid username or password");
        }
    }

    public void displayUserDetails() {
        if(sessionManager.isAuthenticated()) {
            printer.println(sessionManager.displayUserDetails());
        } else {
            printer.println("Invalid login credentials");
        }
    }
}